<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\UserRequest;
use App\Models\User;
use App\Traits\Admin\SuccessOrFailure;
use Illuminate\Http\Request;

class UserController extends Controller
{

    use SuccessOrFailure;

    private $service;


    public function index(Request $request)
    {
        $this->canOrFailure('user.index');

        $users = User::query()->with('projectType')->when($request->input('name'), function ($query) use ($request) {
            return $query->where('name', 'like', '%' . $request->input('name') . '%');
        })->when($request->input('mobile'), function ($query) use ($request) {
            return $query->where('mobile', $request->input('mobile'));
        })->orderByDesc('id')->paginate();
        return view('admin.user.index', compact('users'));
    }

    public function create()
    {
        $this->canOrFailure('user.create');
        return view('admin.user.create');
    }

    public function store(UserRequest $request)
    {
        $this->canOrFailure('user.create');
        list($attributes, $project_types) = $this->buildDate($request,'create');
        $user = User::create($attributes);
        if ($project_types) {
            $user->projectType()->sync($project_types);
        }

        return $this->success('', route('admin.user.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure('user.edit');
        $user = $this->getUserOrFailure($id);
        $user_belong_project_type_ids = $user->projectType ? $user->projectType->pluck('id')->toArray() : [];
        $all_project_type_ids = get_project_types()->pluck('id')->toArray();
        $is_belong_all_project_types = !array_diff($user_belong_project_type_ids, $all_project_type_ids);

        return view('admin.user.edit', compact('user', 'is_belong_all_project_types', 'user_belong_project_type_ids'));
    }

    public function buildDate(UserRequest $request, $type, $user = null)
    {
        $password = $request->input('password');
        $project_types = $request->input('project_type') ?? [];
        $data = [
            'wxid' => $request->input('wxid'),
            'role_id' => $request->input('role_id'),
            'view_permission' => $request->input('view_permission'),
        ];
        if ($type == 'create') {
            $create_data = [
                'name' => $request->input('name'),
                'mobile' => $request->input('mobile'),
                'email' => $request->input('email'),
                'password' => bcrypt($password),
            ];
            $data = array_merge($data, $create_data);
        } else {
            if ($password) {
                $data['password'] = bcrypt($password);
            }
            if ($user->name != $request->input('name')) {
                $data['name'] = $request->input('name');
            }
            if ($user->mobile != $request->input('mobile')) {
                $data['mobile'] = $request->input('mobile');
            }
            if ($user->email != $request->input('email')) {
                $data['email'] = $request->input('email');
            }
        }

        return [$data, $project_types];
    }

    public function update(UserRequest $request, $id)
    {

        $this->canOrFailure('user.edit');
        $user = $this->getUserOrFailure($id);

        list($attributes, $project_types) = $this->buildDate($request,'update',$user);
        $user->update($attributes);
        $user->projectType()->sync($project_types);

        return $this->success('', $request->get('back_url') ?: route('admin.user.index'));
    }


    public function enable($id)
    {
        $this->canOrFailure('user.enable');
        $user = $this->getUserOrFailure($id);
        $user->update(['enabled' => 1]);
        return $this->success();
    }

    public function disable($id)
    {
        $this->canOrFailure('user.disable');
        $user = $this->getUserOrFailure($id);
        $user->update(['enabled' => 0]);
        return $this->success();
    }


    public function destroy($id)
    {
        $this->canOrFailure('user.delete');
        $user = $this->getUserOrFailure($id);
        $user->delete();
        return $this->success();
    }

    private function getUserOrFailure($id)
    {
        $user = User::find($id);
        if (!$user) {
            response($this->failure('用户不存在！'), 404)->send();
            exit;
        }
        return $user;
    }
}
